﻿using System;
using System.Reflection.Metadata;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using Consul;
using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    public class GetPurchaseHandler : IRequestHandler<GetPurchaseCommand, APIResult<List<PurchaseDTO>>>
    {
        private readonly IBaseRepository<MaterialSupplier> _materialSupplier;
        private readonly IBaseRepository<PurchaseInvoice> _purchaseInvoice;

        public GetPurchaseHandler(IBaseRepository<PurchaseInvoice> purchaseInvoice, IBaseRepository<MaterialSupplier> materialSupplier)
        {
            _purchaseInvoice = purchaseInvoice;
            _materialSupplier = materialSupplier;
        }

        public Task<APIResult<List<PurchaseDTO>>> Handle(GetPurchaseCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PurchaseDTO>> result=new APIResult<List<PurchaseDTO>>();
            result.Code = APIEnum.查询成功;
            result.Msg=APIEnum.查询成功.ToString();
            var list = from a in _materialSupplier.GetAll().Result
                       join b in _purchaseInvoice.GetAll().Result on a.Supid equals b.Supid
                       select new PurchaseDTO
                       {
                           Peid=b.Peid,
                           DocumentCode=b.DocumentCode,
                           DocumentTime=b.DocumentTime,
                           DocumentStuct = b.DocumentStuct,
                           BusinessStuct=b.BusinessStuct,
                           Documentid = b.Documentid,
                           Auditor = b.Auditor,
                           Operator = b.Operator,
                           AuditDate = b.AuditDate,
                           Supid=b.Supid,
                           SupplierName = a.SupplierName,
                           TaxNumber = b.TaxNumber,
                           InvoiceType=b.InvoiceType,
                           InvoiceCode= b.InvoiceCode,
                           InvoiceNumber = b.InvoiceNumber,
                           InvoiceDate= b.InvoiceDate,
                           Redandblue = b.Redandblue,
                           InvoiceAmount=b.InvoiceAmount,
                           TaxLv=b.TaxLv,
                           TaxAmount = b.TaxAmount,
                           TotalIncludingTax = b.TotalIncludingTax,
                           FinancialStatus = b.FinancialStatus,
                           material_code=b.material_code
                       };
            if (request.DocumentCode!=0)
            {
                list = list.Where(x => x.DocumentCode==request.DocumentCode);
            }
            if (!string.IsNullOrEmpty(request.InvoiceNumber))
            {
                list = list.Where(x => x.InvoiceNumber.Contains(request.InvoiceNumber));
            }
            if (request.BeginDate != null && request.BeginDate.ToString() != "")
            {
                list = list.Where(x => x.InvoiceDate >= request.BeginDate);
            }
            if (request.EndDate != null && request.EndDate.ToString() != "")
            {
                list = list.Where(x => x.InvoiceDate <= Convert.ToDateTime(request.EndDate).AddDays(1).AddMilliseconds(-1));
            }
            result.data=list.ToList();

            return Task.FromResult(result);
        }
    }
}
